package com.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.classfraction.ClassFractionGetDto;
import com.teacher.pojo.entity.ClassFraction;
import com.teacher.mapper.ClassFractionMapper;
import com.teacher.pojo.entity.MoralEducationBasic;
import com.teacher.pojo.entity.PerStudent;
import com.teacher.pojo.vo.classfraction.ClassFractionGetVo;
import com.teacher.pojo.vo.moraleducationbaisc.MoralEducationBasicGetVo;
import com.teacher.service.IClassFractionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teacher.service.IPerStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 班级评分 服务实现类
 * </p>
 *
 * @author zfm
 * @since 2021-08-13
 */
@Service
public class ClassFractionServiceImpl extends ServiceImpl<ClassFractionMapper, ClassFraction> implements IClassFractionService {

    @Autowired
    private IPerStudentService iPerStudentService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 初始化班级评分
     *
     * @param classId
     * @return
     */
    @Override
    public CommonResult<String> init(Long classId) {
        List<PerStudent> studentList = iPerStudentService.list(new QueryWrapper<PerStudent>().lambda()
                .eq(PerStudent::getClassId, classId));
        List<ClassFraction> classFractionList = new ArrayList<>();
        Long currentTermId = termMapper.getCurrentTerm();
        List<String> studentKeyList = studentList.stream().map(PerStudent::getStudentKey).collect(Collectors.toList());

        if (studentList.size() > 0) {
            // 删除原有的数据
            this.remove(new QueryWrapper<ClassFraction>().lambda()
                    .eq(ClassFraction::getTermId, currentTermId)
                    .in(ClassFraction::getStudentKey, studentKeyList));
            for (PerStudent perStudent : studentList) {
                ClassFraction classFraction = new ClassFraction();
                classFraction.setTermId(currentTermId);
                classFraction.setGroupFraction(new BigDecimal(1));
                classFraction.setTeacherFraction(new BigDecimal(2));
                classFraction.setStudentKey(perStudent.getStudentKey());
                classFractionList.add(classFraction);
            }
            if (classFractionList.size() > 0) {
                this.saveBatch(classFractionList);
            }
        }

        return CommonResult.success("初始化成功");
    }

    /**
     * 查询班级评分
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<IPage<ClassFractionGetVo>> getPage(ClassFractionGetDto dto) {
        Page<ClassFractionGetVo> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
        if (dto.getTermId() == null) {
            dto.setTermId(termMapper.getCurrentTerm());
        }
        IPage<ClassFractionGetVo> iPage = baseMapper.getPage(page, dto);
        return CommonResult.success(iPage);
    }
}
